﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        string sftName = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo info = new DirectoryInfo(textBox1.Text);

            FileInfo[] fiSft = info.GetFiles("*.sft");
            if (fiSft.Length >= 2 && sftName == null)
            {
                foreach (FileInfo sfts in fiSft)
                {
                    sftName = sfts.Name;
                    comboBox2.Items.Add(sfts.Name);
                }
                comboBox2.Visible = true;

                MessageBox.Show("You must select a .sft to use, there are more than one, this is not normal. This value will be appended to the CODEBASE path.");
            }
            else if (fiSft.Length == 1 || sftName.Length >= 6)
            {
                if (fiSft.Length == 1)
                {
                    comboBox2.Visible = false;
                }

                foreach (FileInfo sfts in fiSft)
                {
                    comboBox2.Items.Add(sfts.Name);
                    sftName = sfts.Name;
                }

                FileInfo[] files = info.GetFiles("*.osd");
                foreach (FileInfo fi in files)
                {
                    XmlDocument doc = new XmlDocument();

                    doc.Load(fi.FullName);


                    XmlElement element = doc.SelectSingleNode("/SOFTPKG/IMPLEMENTATION/CODEBASE") as XmlElement;

                    if (comboBox1.Text == "FILE")
                    {
                        element.SetAttribute("HREF", comboBox1.Text + "://" + textBox2.Text + "\\" + sftName);
                    }
                    else
                    {
                        element.SetAttribute("HREF", comboBox1.Text + "://" + txtServer.Text + ":" + txtPort.Text + txtPath.Text + "/" + sftName);
                    }

                    try
                    {
                        fi.Delete();
                        toolStatus.Text = "Deleting " + fi.Name;
                    }
                    catch (System.Exception error)
                    {
                        MessageBox.Show(error.ToString());
                    }

                    try
                    {
                        doc.Save(fi.FullName);
                        toolStatus.Text = "Saving " + fi.Name;
                    }
                    catch (System.Exception error)
                    {
                        MessageBox.Show(error.ToString());
                    }

                    toolStatus.Text = "Done updating files.";
                }
            }
            else
            {
                MessageBox.Show("Unsure how to proceed.");
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.Text)
            {
                case "FILE":
                    textBox2.Visible = true;
                    txtServer.Visible = false;
                    txtPort.Visible = false;
                    txtPath.Visible = false;
                break;

                case "HTTP":
                    textBox2.Visible = false;
                    txtServer.Visible = true;
                    txtPort.Visible = true;
                    txtPath.Visible = true;
                    txtPort.Text = "80";
                break;

                case "HTTPS":
                    textBox2.Visible = false;
                    txtServer.Visible = true;
                    txtPort.Visible = true;
                    txtPath.Visible = true;
                    txtPort.Text = "443";
                break;

                case "RTSP":
                    textBox2.Visible = false;
                    txtServer.Visible = true;
                    txtPort.Visible = true;
                    txtPath.Visible = true;
                    txtPort.Text = "554";
                break;

                case "RTSPS":
                    textBox2.Visible = false;
                    txtServer.Visible = true;
                    txtPort.Visible = true;
                    txtPath.Visible = true;
                    txtPort.Text = "332";
                break;

            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            sftName = comboBox2.Text;
        }
    }
}
